*WHAT'S ACROSS THE BORDER?
*JHA, NEUMARK, AND RODRIGUEZ-LOPEZ

include directories.do


***********************************************
*Rolling regressions

***********************************************

foreach u in "czonestate" "county" {

* Load stacked pair data at the county or czone-state level
use "`datadir'`s'cbp_stacked_`u'pair_sample.dta", clear

if "`u'" == "county" {
merge m:1 countypair_id county using "`countydir'`s'countypairtypes.dta"
keep if type0==1
drop type*
drop _merge
}

************************************************
*******COUNTY-PAIRS OR MULTI-STATA CZONE PAIRS
************************************************

*** PAIR ID VARIABLE (EITHER CZONESTATEPAIR OR COUNTYPAIR)
local pair `u'pair


*Entity variable (EITHER CZONESTATE OR COUNTY)
local geo `u'

***********************************************
*GENERATE VARIABLES (SAME FOR COUNTY OR CZONE)
***********************************************


gen lmw=log(mw)

*Generate totempm, which removes emp of the industry of interest
foreach b in "emp" {
gen tot`b'm=tot`b'-`b'
}

foreach b in "emp" "wageap" {

gen l`b'=log(`b')
gen ltot`b'=log(tot`b')
gen ltot`b'm=log(tot`b'm)
}

gen lworkagepop=log(workagepop)


********************************************
**** DEFINE LOCAL VARIABLES
********************************************

local period year

*Dependent variable and regressors (SAME FOR COUNTY OR CZONE)

local mwvar lmw
local popvar lworkagepop
local wagevar lwageap
local empvar lemp
local totempvar ltotempm
local totwagevar ltotwageapm


**************************************************
****STACKED REGRESSIONS WITH REGHDFE
**************************************************

sort  `pair' `period'
 
gen state_a = statea
gen state_b = stateb

gen st_min = min(state_a, state_b)
gen st_max = max(state_a, state_b)
egen bordersegment = group(st_min st_max)
egen pairperiod=group(`pair' `period')


*USE DATA UP TO 2006
forval j=2006/2016 {
local window`j' "`period'>=1990 & `period'<=`j'"

*Specify clustering
local options "vce(cluster state bordersegment) absorb(`u' pairperiod)"

*Specifications

foreach h in "emp" {

local controls "`tot`h'var' `popvar'"
local entityfe "i.`u'"

eststo m`u'_1_`j': reghdfe ``h'var' `mwvar' `controls' if (`window`j''), `options'

eststo m`u'_2_`j': reghdfe ``h'var' `mwvar' `controls' if pairtimes==2 & (`window`j''),  `options'
}


}

}


tempfile specs

#delimit ;
esttab mczonestate_1* using "`specs'.csv", ///
    nodepvars ///
    collabels(, none) ///
    mlabels(, none) ///
    b(%9.3f) se(%9.3f) ///
    star(* .10 ** .05 *** .01) ///
    title("czonestate-full") ///
	keep(`mwvar') ///
	noobs ///
    nonotes ///
    label ///
    replace;
#delimit cr

#delimit ;
esttab mczonestate_2* using "`specs'.csv", ///
    nodepvars ///
    collabels(, none) ///
    mlabels(, none) ///
    b(%9.3f) se(%9.3f) ///
    star(* .10 ** .05 *** .01) ///
    title("czonestate-dlr") ///
	keep(`mwvar') ///
	noobs ///
    nonotes ///
    label ///
    append;
#delimit cr

#delimit ;
esttab mcounty_1* using "`specs'.csv", ///
    nodepvars ///
    collabels(, none) ///
    mlabels(, none) ///
    b(%9.3f) se(%9.3f) ///
    star(* .10 ** .05 *** .01) ///
    title("countypair-full") ///
    keep(`mwvar' ) ///
	noobs ///
    nonotes ///
    label ///
    append;
#delimit cr

#delimit ;
esttab mcounty_2* using "`specs'.csv", ///
    nodepvars ///
    collabels(, none) ///
    mlabels(, none) ///
    b(%9.3f) se(%9.3f) ///
    star(* .10 ** .05 *** .01) ///
    title("countypair-dlr") ///
    keep(`mwvar') ///
	noobs ///
    nonotes ///
    label ///
    append;
#delimit cr

insheet using "`specs'.csv", comma clear
export excel "`outdir'`s'TablesJNR.xlsx", sheetreplace sheet(Table4)


clear all



